﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;


namespace PRE.Catalog
{
    public partial class FrmBookInfo : DevExpress.XtraEditors.XtraForm
    {
        #region Variable

        DAL.EntitiesDAL.TheLoaiDAL _dal = new DAL.EntitiesDAL.TheLoaiDAL();
        BLL.EntitiesBLL.TheLoaiBLL _bll = new BLL.EntitiesBLL.TheLoaiBLL();

        DataTable _dtdata = new DataTable();
        DataTable _dtTheLoai = new DataTable();

        #endregion

        #region Init

        #region FrmBookInfo
        public FrmBookInfo()
        {
            InitializeComponent();
        }
        #endregion

        #region FrmBookInfo_Load
        private void FrmBookInfo_Load(object sender, EventArgs e)
        {
            GetTheLoai();
        }
        #endregion

        #endregion

        #region Functions

        #region GetTheLoai()
        private void GetTheLoai()
        {    
            _dtTheLoai = _dal.Select();
            lookUpEdit_TheLoai.Properties.DataSource = _dtTheLoai;
        }
        #endregion

        #endregion

        #region Events

        #region private void textEdit_TenSach_KeyDown(object sender, KeyEventArgs e)
        private void textEdit_TenSach_KeyDown(object sender, KeyEventArgs e)
        {
            try
           {
                if (e.KeyData == Keys.Enter)
                {
                    if (textEdit_TenSach.Text == string.Empty)
                    {
                        XtraMessageBox.Show("Chưa nhập tên sách.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    _dtdata = _dal.Select_TenSach(lookUpEdit_TheLoai.Text, textEdit_TenSach.Text
                        , radioGroupLoaiTraCuu.SelectedIndex.ToString(), radioGroupHienTrangSach.SelectedIndex.ToString());

                    if (_dtdata.Rows.Count == 0)
                    {
                        gridControl_TraCuuSach.DataSource = null;
                        XtraMessageBox.Show("Không tìm thấy sách. Kiểm tra lại tên sách hoặc thể loại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    foreach (DataColumn dc in _dtdata.Columns)
                        dc.ReadOnly = true;

                    _dtdata.Columns.Add("IsCheck", typeof(bool));
                    _dtdata.Columns["IsCheck"].ReadOnly = false;
                    foreach (DataRow dr in _dtdata.Rows)
                    {
                        dr["IsCheck"] = false;
                    }

                    gridControl_TraCuuSach.DataSource = _dtdata;

                    try
                    {
                        AppGridView.SummaryField(gridView_TraCuuSach, "MACUONSACH", "Tổng = {0:#,0}", DevExpress.Data.SummaryItemType.Count);

                        if (radioGroupHienTrangSach.SelectedIndex == 1 && radioGroupLoaiTraCuu.SelectedIndex == 0)
                             gridView_TraCuuSach.Columns["STT_PM"].Visible = true;
                        if (radioGroupHienTrangSach.SelectedIndex == 0 && radioGroupLoaiTraCuu.SelectedIndex == 0)
                            gridView_TraCuuSach.Columns["STT_PM"].Visible = false;

                        gridView_TraCuuSach.OptionsView.EnableAppearanceEvenRow = true;
                        gridView_TraCuuSach.OptionsView.ColumnAutoWidth = true;
                        gridView_TraCuuSach.BestFitColumns();
                    }
                    catch { }
                }
            }
            catch { }
        }
        #endregion

        #region private void radioGroupLoaiTraCuu_SelectedIndexChanged(object sender, EventArgs e)
        private void radioGroupLoaiTraCuu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroupLoaiTraCuu.SelectedIndex == 1)
            {
                gridControl_TraCuuSach.DataSource = null;
                layoutControlItem_TheLoai.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layoutControlItem_TenSach.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                try
                {
                    _dtdata = _dal.Select_TenSach(lookUpEdit_TheLoai.Text, textEdit_TenSach.Text
                        , radioGroupLoaiTraCuu.SelectedIndex.ToString(), radioGroupHienTrangSach.SelectedIndex.ToString());

                    if (_dtdata.Rows.Count == 0)
                    {
                        gridControl_TraCuuSach.DataSource = null;
                        XtraMessageBox.Show("Không tìm thấy sách. Kiểm tra lại tên sách hoặc thể loại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    foreach (DataColumn dc in _dtdata.Columns)
                        dc.ReadOnly = true;

                    _dtdata.Columns.Add("IsCheck", typeof(bool));
                    _dtdata.Columns["IsCheck"].ReadOnly = false;
                    foreach (DataRow dr in _dtdata.Rows)
                    {
                        dr["IsCheck"] = false;
                    }

                    gridControl_TraCuuSach.DataSource = _dtdata;

                    try
                    {
                        AppGridView.SummaryField(gridView_TraCuuSach, "MACUONSACH", "Tổng = {0:#,0}", DevExpress.Data.SummaryItemType.Count);

                        if(radioGroupHienTrangSach.SelectedIndex == 1)
                            gridView_TraCuuSach.Columns["STT_PM"].Visible = true;
                        else
                            gridView_TraCuuSach.Columns["STT_PM"].Visible = false;

                        gridView_TraCuuSach.OptionsView.EnableAppearanceEvenRow = true;
                        gridView_TraCuuSach.OptionsView.ColumnAutoWidth = true;
                        gridView_TraCuuSach.BestFitColumns();
                    }
                    catch { }
                    }
                catch { }
                }
            else
            {
                gridControl_TraCuuSach.DataSource = null;
                layoutControlItem_TheLoai.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                layoutControlItem_TenSach.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }
        }
        #endregion

        #region private void btn_Refresh_Click(object sender, EventArgs e)
        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            gridControl_TraCuuSach.DataSource = null;
        }
        #endregion

        #region private void radioGroupHienTrangSach_SelectedIndexChanged(object sender, EventArgs e)
        private void radioGroupHienTrangSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroupHienTrangSach.SelectedIndex == 0 && radioGroupLoaiTraCuu.SelectedIndex == 1)
            {
                gridControl_TraCuuSach.DataSource = null;
                layoutControlItem_TheLoai.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layoutControlItem_TenSach.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                try
                {
                    _dtdata = _dal.Select_TenSach(lookUpEdit_TheLoai.Text, textEdit_TenSach.Text
                        , radioGroupLoaiTraCuu.SelectedIndex.ToString(), "0");

                    if (_dtdata.Rows.Count == 0)
                    {
                        gridControl_TraCuuSach.DataSource = null;
                        XtraMessageBox.Show("Không tìm thấy sách. Kiểm tra lại tên sách hoặc thể loại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    foreach (DataColumn dc in _dtdata.Columns)
                        dc.ReadOnly = true;

                    _dtdata.Columns.Add("IsCheck", typeof(bool));
                    _dtdata.Columns["IsCheck"].ReadOnly = false;
                    foreach (DataRow dr in _dtdata.Rows)
                    {
                        dr["IsCheck"] = false;
                    }

                    gridControl_TraCuuSach.DataSource = _dtdata;

                    try
                    {
                        AppGridView.SummaryField(gridView_TraCuuSach, "MACUONSACH", "Tổng = {0:#,0}", DevExpress.Data.SummaryItemType.Count);

                            gridView_TraCuuSach.Columns["STT_PM"].Visible = false;
                        gridView_TraCuuSach.OptionsView.EnableAppearanceEvenRow = true;
                        gridView_TraCuuSach.OptionsView.ColumnAutoWidth = true;
                        gridView_TraCuuSach.BestFitColumns();
                    }
                    catch { }
                }
                catch { }
            }

            if (radioGroupHienTrangSach.SelectedIndex == 1 && radioGroupLoaiTraCuu.SelectedIndex == 1)
            {
                gridControl_TraCuuSach.DataSource = null;
                layoutControlItem_TheLoai.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                layoutControlItem_TenSach.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                try
                {
                    _dtdata = _dal.Select_TenSach(lookUpEdit_TheLoai.Text, textEdit_TenSach.Text
                        , radioGroupLoaiTraCuu.SelectedIndex.ToString(), "1");

                    if (_dtdata.Rows.Count == 0)
                    {
                        gridControl_TraCuuSach.DataSource = null;
                        XtraMessageBox.Show("Không tìm thấy sách. Kiểm tra lại tên sách hoặc thể loại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    foreach (DataColumn dc in _dtdata.Columns)
                        dc.ReadOnly = true;

                    _dtdata.Columns.Add("IsCheck", typeof(bool));
                    _dtdata.Columns["IsCheck"].ReadOnly = false;
                    foreach (DataRow dr in _dtdata.Rows)
                    {
                        dr["IsCheck"] = false;
                    }

                    gridControl_TraCuuSach.DataSource = _dtdata;

                    try
                    {
                        AppGridView.SummaryField(gridView_TraCuuSach, "MACUONSACH", "Tổng = {0:#,0}", DevExpress.Data.SummaryItemType.Count);

                            gridView_TraCuuSach.Columns["STT_PM"].Visible = true;

                        gridView_TraCuuSach.OptionsView.EnableAppearanceEvenRow = true;
                        gridView_TraCuuSach.OptionsView.ColumnAutoWidth = true;
                        gridView_TraCuuSach.BestFitColumns();
                    }
                    catch { }
                }
                catch { }
            }
        }
        #endregion

        #region private void checkEdit_ChonTatCa_CheckedChanged(object sender, EventArgs e)
        private void checkEdit_ChonTatCa_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < gridView_TraCuuSach.DataRowCount; i++)
            {
                gridView_TraCuuSach.GetDataRow(i)["IsCheck"] = checkEdit_ChonTatCa.Checked;
            }
        }
        #endregion

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}
